var login_info_obj;

$(function(){
	showLoginInfo();
	showMenu();
})

layui.use('element',function(){
	let element = layui.element;
	
	$('#show-index').click(function(){
		element.tabChange('xbs_tab', '0');
	})
})

function showLoginInfo(){
	$.ajax({
		type : 'get',
    	url : access_url+'/index/homepage/getlogininfofromcache',
    	dataType : 'json',
    	success : function(data){
    		if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
    		var loginInfo = data.retdata;
    		
    		login_info_obj = loginInfo;
    		
    		console.log("loginInfo",loginInfo);
    		
    		var person_name = loginInfo.person.person_name;
    		
    		console.log("person_name",person_name);
    		
    		$('#hello_show').text(person_name);
    	}
	})
}
    
//加载菜单列表
function showMenu(){
	$.ajax({
		type : 'get',
    	url : access_url+'/index/homepage/getloginmenus',
    	contentType : 'application/json;charset=utf-8',
    	success : function(data){
    		if(data.code!=200){
    			layer.msg(data.msg,{icon:2});
    			return;
    		}
    		var menus = data.retdata;
    		if(isNull(menus)){
    			console.log('菜单列表为空');
    			return;
    		}
    		var firstMenuhtml = '';
    		var menuhtml = '';
    		var menuNum = menus.length;
    		
    		for (var i = 0; i < menuNum; i++) {
				var menu = menus[i];
				var menuNodeHtml = getMenuHtml(menu);
				menuhtml += menuNodeHtml;
			}
    		
    		$('#nav').append(menuhtml);
    	}
	})
}

//递归算法获取菜单树
function getMenuHtml(menu){
	var menuHtml = '';
	var menu_urlst = menu.menu_url
	if(isNull(menu_urlst)){
		menu_urlst = 'javascript:;';
	}
	
	menuHtml += `<li><a _href="${menu_urlst}"><i class="iconfont">${menu.menu_icon}</i><cite>${menu.menu_name}</cite>`
		
	var childs = menu.childs;
	
	var childHtml = '';
	
	if(!isNull(childs)){
		menuHtml += '<i class="iconfont nav_right">&#xe697;</i></a>';
		menuHtml += '<ul class="sub-menu">';
		var childNum = childs.length;
		for (var i = 0; i < childNum; i++) {
			var child = childs[i];
			childHtml += getMenuHtml(child);
		}
		menuHtml += childHtml;
		menuHtml += '</ul>';
	} else {
		menuHtml += '</a>';
	}
	
	menuHtml += '</li>';
	return menuHtml;
}

function showLayer(){
	$.ajax({
		type : 'get',
    	url : access_url+'/index/homepage/getloginmenus',
    	contentType : 'application/json;charset=utf-8',
    	success : function(data){
    		console.log('返回数据',JSON.stringify(data.retdata));
    		layer.msg(JSON.stringify(data.retdata));
    	}
	})
}

//登出
function logOut(){
	layer.confirm('您确定要退出系统？', {
		btn: ['确定','取消'] //按钮
	}, function(){
		$.ajax({
			type : 'get',
        	url : access_url+'/auth/login/logout',
        	contentType : 'application/json;charset=utf-8',
        	success : function(data){
        		if(data.code!=200){
        			layer.msg(data.msg,{icon:2});
        			return;
        		}
        		location.href='login.html'
        	}
		})
	}, function(){
	  
	});
}